package geppetto.main;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;

public class MergeCountTableEntries {
	
	public static void main(String[] args) throws IOException {
	String input = args[0];
	String output = args[1];
	String encoding = "UTF8";
	BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(input), encoding));
	PrintStream writer = new PrintStream(new File(output), encoding);

	String[] currentExtract = reader.readLine().split("\\s+\\|\\|\\|\\s+");
	int numberOfVariables = currentExtract.length - 1;
	String[] values = currentExtract[numberOfVariables].split("\\s+");
	int numberOfFeatures = values.length;
	double[] currentValues = new double[numberOfFeatures];
	for(int i = 0; i < numberOfFeatures; i++){
		currentValues[i]=Double.parseDouble(values[i]);
	}
	while(reader.ready()){
		String line = reader.readLine();
		String[] extract = line.split("\\s+\\|\\|\\|\\s+");
		boolean sameVar = true;
		if(numberOfVariables != extract.length - 1){
			sameVar = false;
		}
		for(int i = 0; i < numberOfVariables && sameVar; i++){
			if(!extract[i].equals(currentExtract[i])){
				sameVar=false;
			}
		}
		if(sameVar){
			for(int i = 0; i < numberOfFeatures; i++){
				values = extract[numberOfVariables].split("\\s+");
				currentValues[i]+=Double.parseDouble(values[i]);
			}
		}
		else{
			for(int i = 0; i < numberOfVariables; i++){
				writer.print(currentExtract[i] + " ||| ");				
			}
			for(int i = 0; i < numberOfFeatures; i++){				
				writer.print(currentValues[i] + " ");
			}
			writer.println();			
			currentExtract = extract;
			numberOfVariables = currentExtract.length -1 ;
			values = currentExtract[numberOfVariables].split("\\s+");
			numberOfFeatures = values.length;
			currentValues = new double[numberOfFeatures];
			for(int i = 0; i < numberOfFeatures; i++){
				currentValues[i]=Double.parseDouble(values[i]);
			}
		}
	}
	for(int i = 0; i < numberOfVariables; i++){
		writer.print(currentExtract[i] + " ||| ");				
	}
	for(int i = 0; i < numberOfFeatures; i++){				
		writer.print(currentValues[i] + " ");
	}
}
}
